package com.jfree.zuul.filter;

import com.jfree.entity.general.ResponseJson;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class ErrorTokenFilter extends ZuulFilter {
    private static final String ERROR_STATUS_CODE_KEY = "error.status_code";

    private Logger log = LoggerFactory.getLogger(ErrorTokenFilter.class);

    public static final String DEFAULT_ERR_MSG = "系统繁忙,请稍后再试";

    @Override
    public String filterType() {
        return FilterConstants.ERROR_TYPE;
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        RequestContext ctx = RequestContext.getCurrentContext();
        //return ctx.containsKey(ERROR_STATUS_CODE_KEY);
        return true;
    }

    @Override
    public Object run() {
        log.info("---err filter---");
        RequestContext ctx = RequestContext.getCurrentContext();
        ResponseJson rn = new ResponseJson();
        try {
            HttpServletRequest request = ctx.getRequest();

            int statusCode = (Integer) ctx.get(ERROR_STATUS_CODE_KEY);
            String message = (String) ctx.get("error.message");
            if (ctx.containsKey("error.exception")) {
                Throwable e = (Exception) ctx.get("error.exception");
                Throwable re = getOriginException(e);
                if (re instanceof java.net.ConnectException) {
                    message = "Real Service Connection refused";
                    log.warn("uri:{},error:{}", request.getRequestURI(), re.getMessage());
                } else if (re instanceof java.net.SocketTimeoutException) {
                    message = "Real Service Timeout";
                    log.warn("uri:{},error:{}", request.getRequestURI(), re.getMessage());
                } else if (re instanceof com.netflix.client.ClientException) {
                    message = re.getMessage();
                    log.warn("uri:{},error:{}", request.getRequestURI(), re.getMessage());
                } else {
                    log.warn("Error during filtering", e);
                }
            }

            if (StringUtils.isBlank(message)) message = DEFAULT_ERR_MSG;

            request.setAttribute("javax.servlet.error.status_code", statusCode);
            request.setAttribute("javax.servlet.error.message", message);
            rn.setCode(statusCode);
            rn.setMessage(message);

        } catch (Exception e) {
            String error = "Error during filtering[ErrorFilter]";
            log.error(error, e);
            rn.setCode(500);
            rn.setMessage(e.toString());
        }
        try {
            ctx.getResponse().getWriter().print(rn.toString());
        } catch (IOException e) {
            log.error("json格式化错误:{}", e.toString());
        }
        return null;

    }

    private Throwable getOriginException(Throwable e) {
        e = e.getCause();
        while (e.getCause() != null) {
            e = e.getCause();
        }
        return e;
    }
}